<template>
    <el-container class="home-container">
        <!-- 头部区域 -->
        <el-header>
            <div>
                <img src="../assets/logo.png" />
                <span>电商后台管理系统</span>
            </div>
            <el-button
                type="info"
                @click="logout"
            >退出</el-button>
        </el-header>
        <!-- 页面主体区域 -->
        <el-container>
            <!-- 侧边栏 -->
            <el-aside :width="isCollapse ? '64px' : '200px'">
                <!-- 折叠菜单栏按钮 -->
                <div
                    class="toggle-button"
                    @click="toggleCollapse"
                >|||</div>
                <!-- 侧边栏菜单区 -->
                <el-menu
                    background-color="#333744"
                    text-color="#fff"
                    active-text-color="#409eff"
                    :unique-opened="true"
                    :collapse-transition="false"
                    :collapse="isCollapse"
                    :router="true"
                    :default-active="activePath"
                >
                    <!-- 一级菜单 -->
                    <el-submenu
                        :index="items.id+''"
                        v-for="items in menuList"
                        :key="items.id"
                    >
                        <!-- 一级菜单模板 -->
                        <template slot="title">
                            <!-- 图标 -->
                            <i :class="iconsObj[items.id]"></i>
                            <!-- 文本 -->
                            <span>{{items.authName}}</span>
                        </template>
                        <!-- 二级菜单模板 -->
                        <el-menu-item
                            :index="'/'+ item.path"
                            v-for="item in items.children"
                            :key="item.id"
                            @click="saveNavState('/'+ item.path)"
                        >
                            <i class="el-icon-menu"></i>
                            <span>{{item.authName}}</span>
                        </el-menu-item>
                        <!-- 多级菜单模板 -->
                        <!-- <el-submenu index="1-4">
                            <template slot="title">选项4</template>
                            <el-menu-item index="1-4-1">选项1</el-menu-item>
                        </el-submenu> -->
                    </el-submenu>
                </el-menu>
            </el-aside>
            <!-- 右边内容主体 -->
            <el-main>
                <!-- 路由占位符 -->
                <router-view></router-view>
            </el-main>
        </el-container>
    </el-container>
</template>

<script>
export default {
    data () {
        return {
            // 左侧菜单数据
            menuList: [],
            // 左侧菜单一级菜单图标
            iconsObj: {
                '125': 'iconfont icon-icon_users',
                '103': 'iconfont icon-tijikongjian',
                '101': 'iconfont icon-shangpingouwudai2',
                '102': 'iconfont icon-danju-tianchong',
                '145': 'iconfont icon-baobiao'
            },
            // 是否折叠菜单栏
            isCollapse: false,
            // 被激活的链接地址
            activePath: ''
        }
    },
    created () {
        this.activePath = this.$route.path
        this.getMenuList()
    },
    watch: {
        $route: {
            handler: function (val, oldVal) {
                this.activePath = this.$route.path
            }
        }
    },
    methods: {
        // 获取所有菜单
        getMenuList () {
            this.$request({
                url: 'menus',
                method: 'get'
            }).then(res => {
                this.menuList = res
            })
        },
        // 切换菜单折叠与展开
        toggleCollapse () {
            this.isCollapse = !this.isCollapse
        },
        // 保存链接的激活状态
        saveNavState (activePath) {
            this.activePath = activePath
        },
        // 退出登录
        logout () {
            this.$messageBox
                .confirm('是否确认退出登录?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                })
                .then(() => {
                    this.$message({
                        type: 'success',
                        message: '退出成功!'
                    })
                    window.sessionStorage.clear()
                    this.$router.push('/login')
                })
                .catch(() => { })
        }
    }
}
</script>

<style lang="less" scoped>
.home-container {
    height: 100%;
}

.el-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background-color: #373d41;
    div {
        display: flex;
        align-items: center;
        img {
            width: 50px;
            height: 50px;
            border-radius: 50%;
        }
        span {
            font-size: 20px;
            color: #fff;
            margin-left: 15px;
        }
    }
}

.el-aside {
    background-color: #333744;
}

.el-main {
    background-color: #eaedf1;
}

.el-menu {
    border-right: none;
}

.iconfont {
    margin-right: 10px;
}

.toggle-button {
    font-size: 10px;
    color: #fff;
    line-height: 24px;
    text-align: center;
    letter-spacing: 0.2em;
    background-color: #4a5064;
    cursor: pointer;
}
</style>